package com.dstats;

import android.content.Context;
import android.database.Cursor;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dstats.datamodel.Hero;
import com.squareup.picasso.Picasso;

public class HeroAdapter extends CursorAdapter {

    private final int density;

    public HeroAdapter(Context context, Cursor c, boolean autoRequery) {
        super(context, c, autoRequery);
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        density = (int) metrics.density;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
        View convertView = LayoutInflater.from(context).inflate(R.layout.hero_view_row, null);
        ViewHolder holder = new ViewHolder();
        holder.heroName = (TextView) convertView.findViewById(R.id.textViewHeroName);
        holder.img = (ImageView) convertView.findViewById(R.id.imageViewHeroImg);
        convertView.setTag(holder);
        return convertView;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        Hero h = new Hero(cursor);
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.heroName.setText(h.getLocalized_name());
        holder.heroName.setGravity(Gravity.CENTER);
        Picasso.with(context).load(h.getImgResString()).into(holder.img);
    }

    private static class ViewHolder {
        private ImageView img;
        private TextView heroName;
    }
}